package com.java.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.dao.UserMapper;
import com.java.model.*;
import com.java.service.PermissionService;
import com.java.service.RoleService;
import com.java.service.UserRoleService;
import com.java.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public Result login(User user, HttpServletRequest request) {
        User log = userMapper.login(user);
        if (log != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", log);


            // 在用户登录成功后查询当前用户拥有的权限并存储（存储在Session中）
            List<Permission> userPermissionList = permissionService.listByLoginId(Integer.valueOf(log.getId()));
            session.setAttribute("userPermissionList", userPermissionList);

            // 查询系统中管理员的所有权限并存储（存储在Session域中）
            Permission permission = new Permission();
            permission.setPageSize(999);
            Result result = permissionService.toList(permission);
            session.setAttribute("permissionList", result.getData());

            return Result.success();
        }

        return Result.error("用户名或密码错误");
    }

    @Override
    public Result get(int id) {
        User user = userMapper.get(id);
        return Result.success(user, null);
    }

    @Override
    public Result save(User user) {
        userMapper.save(user);
        return Result.success();
    }

    @Override
    public Result modify(User user) {
        userMapper.modify(user);
        return Result.success();
    }

    @Override
    public Result list(User user) {
        PageHelper.startPage(user.getPageNum(), user.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据
        List<User> list =  userMapper.list(user);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            userMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result listRole(int userId) {
        // 1.查询所有角色用于显示列表
        Result result = roleService.toList(new Role());
        if (!result.getCode().equals("0")) {
            return result;
        }

        // 2.查询操作的管理员有的权限
        List<UserRole> userRoles = userRoleService.list(userId);
        List<Integer> roleIdList = new ArrayList<>();
        if (userRoles != null && userRoles.size() > 0) {
            for (UserRole userRole : userRoles) {
                roleIdList.add(userRole.getRoleId());
            }
        }

        // 3.遍历所有角色判断操作管理员是否有该权限，如果有则将checked设置为true， 如果没有设置成false
        List<Role> roleList = (List<Role>) result.getData();
        if (roleList != null && roleList.size() > 0) {
            for (Role role : roleList) {
                if (roleIdList.contains(role.getId())) {
                    role.setChecked(true);
                } else {
                    role.setChecked(false);
                }
            }
        }
        return result;
    }
}
